package deibay.client.gui;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.swingx.JXList;

import deibay.client.core.views.IPanelSeller;
import deibay.client.core.views.IViewAdvertise;
import deibay.client.core.views.IViewItemDetailsBidder;
import deibay.client.core.views.IViewItemDetailsSeller;
import deibay.util.Pair;

import java.awt.Point;
import javax.swing.JLabel;
import java.awt.Rectangle;

public class PanelSellerView extends JPanel implements IPanelSeller{

	private JXList itemsList = null;
	JSplitPane splitPane = null;
	private JScrollPane scrollList;
	private JScrollPane scrollDetails;
	private ItemDetailsView itemDetails = null;
	private JLabel addItemLabel = null;
	private JLabel sendAdvertiseLabel = null;
	
	public PanelSellerView()
	{
		super();
		initialize();
	}
	
	private void initialize()
	{		
		sendAdvertiseLabel = new JLabel();
		sendAdvertiseLabel.setBounds(new Rectangle(120, 15, 110, 25));
		sendAdvertiseLabel.setText("Send Advertise");
		sendAdvertiseLabel.setIcon(new ImageIcon(this.getClass().getResource("/images/send_advertise.png")));
		sendAdvertiseLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		sendAdvertiseLabel.setEnabled(false);
		addItemLabel = new JLabel();
		addItemLabel.setBounds(new Rectangle(10, 15, 75, 25));
		addItemLabel.setText("Add Item");
		addItemLabel.setIcon(new ImageIcon(this.getClass().getResource("/images/cart_add.png")));
		addItemLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		addItemLabel.setEnabled(false);
		itemDetails = new ItemDetailsView();
		this.setLayout(null);
		setSize(new Dimension(750, 600));	
		this.add(getSplitPane(), null);
		this.add(addItemLabel, null);
		this.add(sendAdvertiseLabel, null);				
		getItemsComponent().setModel(new DefaultListModel());
		getItemsComponent().setCellRenderer(new ListItemsRenderer());
	}
	
	private JSplitPane getSplitPane()
	{
		if(splitPane == null)
		{
			splitPane = new JSplitPane();
			splitPane.setSize(new Dimension(750, 540));
			splitPane.setDividerLocation(200);
			splitPane.setDividerSize(13);	
			splitPane.setLocation(new Point(5, 50));
			splitPane.setLeftComponent(getScrollListComponent());
			splitPane.setRightComponent(itemDetails);
		}
		
		return splitPane;
	}
	private JScrollPane getScrollListComponent() {
		if(scrollList == null)
		{
			scrollList = new JScrollPane();
			scrollList.setSize(300, 545);
			scrollList.setViewportView(getItemsComponent());
		}
		return scrollList;
	}

	private JScrollPane getScrollDetailsComponent() {
		if(scrollDetails == null)
		{
			scrollDetails = new JScrollPane();			
			scrollDetails.setViewportView(new ItemDetailsView());
		}
		return scrollDetails;
	}
	
	private JXList getItemsComponent()
	{
		if(itemsList == null)
		{
			itemsList = new JXList();			
		}
		return itemsList;
	}
	
	public IViewItemDetailsBidder showItemDetailsView() {
				
		return itemDetails;
	}
	
	public void setItems(Vector<Pair<String,String>> items) {
		Vector<ItemObjectIcon> objectItems = new Vector<ItemObjectIcon>();
		for(Pair<String, String> item: items)
		{
			ItemObjectIcon itemObj = new ItemObjectIcon(item.getFirst(), item.getSecond());
			objectItems.add(itemObj);
		}
		itemsList.setListData(objectItems);
	}

	public void setChangeItemAction(ListSelectionListener listener)
	{
		this.itemsList.addListSelectionListener(listener);
	}
	
	public void setClickItemAction(MouseListener listener)
	{
		this.itemsList.addMouseListener(listener);
	}
	
	public void setBidderViewEnabled(boolean b)
	{
		this.setEnabled(b);
	}

	public void setAddItemAction(MouseListener listener) {
		this.addItemLabel.addMouseListener(listener);		
	}

	public void setSendAdvertiseAction(MouseListener listener) {
		this.sendAdvertiseLabel.addMouseListener(listener);		
	}

	public void setAddItemActionEnabled(boolean b) {		
		this.addItemLabel.setEnabled(b);
	}

	public void setSendAdvertiseActionEnabled(boolean b) {
		this.sendAdvertiseLabel.setEnabled(b);
	}

	public IViewItemDetailsSeller showAddItemView() {
		return new CreateItemView();
	}

	public IViewAdvertise showAdvertiseView() {
		return new AdvertiseView();
	}
}
